Find the fewest number of coins to make a target amount
Find the minimum number of coins needed to make a target amount using an infinite supply of given coin denominations with dynamic programming. Return -1 if impossible.
Coins:
Output: 3 (5+5+1)
Coins:
Output: 0
Coins:
Output: -1
| Amount | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| DP State | 0 | 1 | 1 | 2 |
Output: 2 (ways: [1,1])
Iterate over n coins and amount values
For the DP array
Example 1: coins=[1,2,5], amount=11 → 3 (5+5+1)
Example 2: coins=[1], amount=0 → 0
Example 3: coins=[2], amount=3 → -1
Iterate over n coins and amount values
For the DP array